package br.com.conhecimento.controller;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;
import org.springframework.stereotype.Component;

import br.com.conhecimento.model.Usuario;
import br.com.conhecimento.util.FacesUtil;
import br.com.conhecimento.util.MenuUtil;

/**
 * Bean responsavel pelo menu do usuario 
 * 
 * @author Luis Garcia
 * @since 27/04/2013
 */
@Component
@ManagedBean
@SessionScoped
public class MenuBean extends CommonBean {

	private MenuModel model;
	
	public MenuModel getModel() {
		Usuario usuario = (Usuario) FacesUtil.getSessionMap().get("usuario");
		if (usuario != null) {
			MenuUtil menuUtil = new MenuUtil(usuario.getPerfil().getAcessos());
			model = new DefaultMenuModel();
			for (final br.com.conhecimento.model.Menu menuModel : menuUtil.getParent()) {
				Submenu submenu = new Submenu();
				submenu.setIcon(menuModel.getImg());
				submenu.setLabel(menuModel.getDescricao());
				for (final br.com.conhecimento.model.MenuItem item : menuUtil.getChildren(menuModel)) {
					MenuItem menuItem = new MenuItem();
					menuItem.setValue(item.getDescricao());
					menuItem.setIcon(item.getImgLink());
					menuItem.setUrl(item.getLink());
					submenu.getChildren().add(menuItem);
				}
				model.addSubmenu(submenu);
			}
		}
		return model;
	}
	
}
